/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-12-02 14:12:40
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timestampToTime = require("../utils/timeFormart");
class ArticleService extends Service {
    //获取文章数据 +分页 +搜索
    async getArticle() {
            const { ctx, app } = this;
            const { pageSize, pageIndex, searchVal, state, classifyVal } =
            ctx.request.body;
            let res = await app.mysql.select("allarticle");
            let total = res.length;
            //时间倒序
            res.sort((a, b) => {
                return new Date(b.release_time) - new Date(a.release_time);
            });
            //搜索框搜索

            if (searchVal) {
                res = res.filter((item) => {
                    if (
                        item.title.indexOf(searchVal) !== -1 ||
                        item.title.includes(searchVal)
                    ) {
                        return item;
                    }
                });
                total = res.length;
            }
            //状态搜索
            if (state !== null) {
                res = res.filter((item) => item.state * 1 === state * 1);
                total = res.length;
            }
            //分类搜索
            if (classifyVal) {
                res = res.filter((item) => item.classify === classifyVal);
                total = res.length;
            }
            //分页
            res = res.slice((pageIndex - 1) * pageSize, pageSize * pageIndex);

            return { res, total };
        }
        //最新文章
    async getNewArticle() {
            const { ctx, app } = this;
            let res = await app.mysql.select("allarticle");
            res.sort((a, b) => {
                return (
                    new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
                );
            });
            res = res.slice(0, 6);
            return res;
        }
        //获取详情
    async getDetail() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.get("allarticle", { id });
            return res;
        }
        //编辑内容
    async editDetail() {
            const { ctx, app } = this;
            const {
                id,
                title,
                content,
                tag,
                classify,
                focus,
                cover,
                abstract,
                visit_pwd,
                pay_check,
                is_comment,
            } = ctx.request.body;
            const res = await app.mysql.update("allarticle", {
                id,
                ...ctx.request.body,
            });
            return res;
        }
        //新增文章
    async addArticle() {
        const { ctx, app } = this;
        const {
            title,
            content,
            tag,
            classify,
            focus,
            cover,
            abstract,
            visit_pwd,
            pay_check,
            is_comment,
            state,
        } = ctx.request.body;
        const res = await app.mysql.insert("allarticle", {
            ...ctx.request.body,
            release_time: timestampToTime(Date.now()),
        });
        return res;
    }
    async getArticleTotal() {
            const { ctx, app } = this;
            const res = await app.mysql.select("allarticle");
            const total = res.length;
            return total;
        }
        //删除
    async delArticle() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.delete("allarticle", {
                id,
            });
            return res;
        }
        //改变首焦状态
    async changeArticleFocus() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            //找到原数据
            const res = await app.mysql.get("allarticle", {
                id,
            });
            //判断状态
            if (res.focus * 1 === 1) {
                //更新首焦状态
                await app.mysql.update("allarticle", { id, focus: 0 });
            } else {
                //更新首焦状态
                await app.mysql.update("allarticle", { id, focus: 1 });
            }
            return "操作成功";
        }
        //批量删除
    async batchDelArticle() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.delete("allarticle", {
                    id: item.id,
                });
            });
            return "操作成功";
        }
        //批量发布
    async batchPubArticle() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("allarticle", {
                    id: item.id,
                    state: 1,
                    release_time: timestampToTime(Date.now()),
                });
            });
            return "操作成功";
        }
        //批量草稿
    async batchDftArticle() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("allarticle", {
                    id: item.id,
                    state: 0,
                });
            });
            return "操作成功";
        } //批量撤销
    async batchRemoveArticle() {
            const { ctx, app } = this;
            const { batchList } = ctx.request.body;
            batchList.forEach((item) => {
                app.mysql.update("allarticle", {
                    id: item.id,
                    focus: 0,
                });
            });
            return "操作成功";
        }
        //批量推荐
    async batchFocusArticle() {
        const { ctx, app } = this;
        const { batchList } = ctx.request.body;
        batchList.forEach((item) => {
            app.mysql.update("allarticle", {
                id: item.id,
                focus: 1,
            });
        });
        return "操作成功";
    }

    // ***********用户端**********************//
    async searchArticle() {
            const { ctx, app } = this;
            const { searchVal } = ctx.query;
            let res = await app.mysql.select("allarticle");
            res = res.filter((item) => item.title.includes(searchVal));
            return res;
        }
        //推荐阅读
    async recommendArticle() {
            const { ctx, app } = this;
            const res = await app.mysql.select("allarticle");
            let result = [];
            res.forEach((item) => {
                if (item.state * 1) {
                    result.push(item);
                }
            });
            result.sort((a, b) => {
                return (
                    new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
                );
            });
            if (result.length >= 6) {
                result = result.slice(0, 6);
            }
            return result;
        }
        //获取上线文章
    async getStateArticle() {
            const { ctx, app } = this;
            const res = await app.mysql.select("allarticle");
            let result = [];
            res.forEach((item) => {
                if (item.state * 1) {
                    result.push(item);
                }
            });
            result.sort((a, b) => {
                return (
                    new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
                );
            });
            return result;
        }
        //首焦推荐轮播图数据
    async getFocusArticle() {
            const { ctx, app } = this;
            const res = await app.mysql.select("allarticle");
            const result = [];
            res.forEach((item) => {
                if (item.state * 1 && item.focus * 1) {
                    result.push(item);
                }
            });
            result.sort((a, b) => {
                return (
                    new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
                );
            });
            return result;
        }
        //阅读量++
    async addArticleReading() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.get("allarticle", { id });
            const looks = res.looks;
            const result = await app.mysql.update("allarticle", {
                id,
                looks: looks + 1,
            });
            return result;
        }
        //点赞
    async addArticleLikes() {
            const { ctx, app } = this;
            const { article_id, ip } = ctx.request.body;
            const res = await app.mysql.get("allarticle", { id: article_id });
            const isHave = await app.mysql.get("like_form", {
                article_id: article_id,
                ip,
            });
            const likes = res.likes;
            if (isHave) {
                await app.mysql.update("allarticle", {
                    id: article_id,
                    likes: likes - 1,
                });
                await app.mysql.delete("like_form", { article_id, ip });
            } else {
                await app.mysql.update("allarticle", {
                    id: article_id,
                    likes: likes + 1,
                });
                await app.mysql.insert("like_form", { article_id, ip });
            }
            return "操作成功";
        }
        //是否点过赞
    async isArticleLikes() {
        const { ctx, app } = this;
        const { article_id, ip } = ctx.request.body;
        const isHave = await app.mysql.get("like_form", {
            article_id: article_id,
            ip,
        });
        if (isHave) return true;
        return false;
    }
}
module.exports = ArticleService;